#include "TextureManager.h"

#include "TextureManager.h"
#include "DebugLog.h"

TextureManager* TextureManager::_instance = NULL;
TextureManager* TextureManager::instance()
{
    if(!_instance)
    {
        _instance = new TextureManager();
    }
    return _instance;
}

/// constructor
TextureManager::TextureManager()
{
}

/// destructor
TextureManager::~TextureManager()
{
    _images.clear();
    if(_instance)
    {
        delete _instance;
    }
}



/// gets an image from the list if it exists or creates one if it dosent
sf::Image& TextureManager::textureImage(std::string image_name)
{
    _iterator = _images.find(image_name);
    if(_iterator == _images.end())
    {
        return _addImage(image_name);
    }
    return _iterator->second;
}


/// adds an image to the list
sf::Image& TextureManager::_addImage(std::string image_name)
{
    sf::Image temp;
    if(!temp.loadFromFile(image_name))
    {
        DebugLog::instance()->write("Image");
        DebugLog::instance()->write(image_name.c_str());
        DebugLog::instance()->writeLine("did not load");
    }
    temp.createMaskFromColor(sf::Color(255,0,255));
    _images.insert(std::make_pair<std::string, sf::Image>(image_name, temp)); // i dont think i need the template brackets but its fine

    _iterator = _images.find(image_name);
    return _iterator->second;
}
